package com.deplume.ssm.controller.admin.home;

import com.deplume.ssm.pojo.admin.Delivery;
import com.deplume.ssm.pojo.admin.DeliveryActivity;
import com.deplume.ssm.pojo.admin.Job;
import com.deplume.ssm.pojo.admin.WorkExp;
import com.deplume.ssm.pojo.common.*;
import com.deplume.ssm.pojo.home.TypeOne;
import com.deplume.ssm.service.inface.admin.home.IHomeService;
import com.deplume.ssm.service.inface.admin.home.IMainService;
import com.deplume.ssm.service.inface.admin.home.IResumeHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by libing on 2016/7/23.
 */
@Controller
@RequestMapping("/main")
public class MainController {

    @Autowired
    IMainService mainService;

    @Autowired
    IHomeService homeService;

    @Autowired
    IResumeHomeService resumeHomeService;

    @RequestMapping("ajaxTypesJsp")
    @ResponseBody
    public MyResult ajaxTypes(@RequestParam("order") int order)throws Exception
    {
        MyResult myResult = new MyResult();
        TypeOne typeOne = mainService.getFirstAndSecondTypes(order);
        if(typeOne != null)
        {
            myResult.setSuccess(true);
            myResult.setMsg("success");
            myResult.setTypeOne(typeOne);
        }
        else
        {
            myResult.setSuccess(false);
            myResult.setMsg("server error");
        }
        return myResult;
    }

    @RequestMapping("ajaxSumTypesJsp")
    @ResponseBody
    public MyResult ajaxSumTypes(@RequestParam("order") int order)throws Exception{
        MyResult myResult = new MyResult();
        TypeOne typeOne = mainService.getSumTypes(order);
        if(typeOne != null)
        {
            myResult.setSuccess(true);
            myResult.setMsg("success");
            myResult.setTypeOne(typeOne);
        }else {
            myResult.setSuccess(false);
        }

        return myResult;
    }
/*
    @RequestMapping("ajaxTypeListJsp")
    @ResponseBody
    public ResultList ajaxTypeList()throws Exception{
        ResultList resultList = new ResultList();
        List<TypeOne> typeOnes = mainService.getSumTypeList();

        if(typeOnes != null)
        {
            resultList.setSuccess(true);
            resultList.setMsg("success");
            resultList.setTypeOnes(typeOnes);
        }
        else {
            resultList.setSuccess(false);
            resultList.setMsg("error");
        }
        return resultList;
    }*/


    /*
    *  按热度返回前15条职位
    */
    @RequestMapping("selectJobsJsp")
    @ResponseBody
    public ResultJobList returnJobList()throws Exception{
        ResultJobList resultJobList = new ResultJobList();
        List<Job> jobs = homeService.selectJobs();

        System.out.println("=================================");
        System.out.println(jobs.size());
        System.out.println(jobs.get(0).getCompanyDetail().getCompanyTags().get(0).getTagName());
        System.out.println("=================================");

        if(jobs != null) {
            resultJobList.setSuccess(true);
            resultJobList.setMsg("success");
            resultJobList.setJobs(jobs);
        }
        else {
            resultJobList.setSuccess(false);
            resultJobList.setMsg("error");
        }
        return resultJobList;
    }

    @RequestMapping("toJobListJsp")
    public String toJobList(HttpSession session,
                            Model model,
                            @RequestParam("typeName") String typeName)throws Exception{
        System.out.println("typeName2:=========="+typeName);
        List<Job> jobs = homeService.selectJob2ByName("%"+typeName+"%");
        if(jobs.size() == 0 || jobs == null)
        {
            model.addAttribute("isNULL",true);
        }
        else {
            model.addAttribute("jobs", jobs);
        }
        model.addAttribute("typeName", typeName);
        model.addAttribute("types",2);
        model.addAttribute("user",session.getAttribute("user"));
        return "home/list";
    }

    @RequestMapping("toJobPlusListJsp")
    public String toJobPlusList(HttpSession session,
                                Model model,
                                @RequestParam("typeName") String typeName)throws Exception{
        System.out.println("typeName3:=========="+typeName);
        List<Job> jobs = homeService.selectJob3ByName("%"+typeName+"%");
        if(jobs.size() == 0 || jobs == null)
        {
            model.addAttribute("isNULL",true);
        }
        else {
            model.addAttribute("jobs", jobs);
        }
        model.addAttribute("typeName", typeName);
        model.addAttribute("types",3);
        model.addAttribute("user",session.getAttribute("user"));
        return "home/list";
    }

    @RequestMapping("searchJobBySalaryJsp")
    public String searchJobBySalary(HttpSession session,
                                    Model model,
                                    @RequestParam("typeName") String typeName,
                                    @RequestParam("type") String type,
                                    @RequestParam("types") String types)throws Exception{
        List<Job> jobs = null;
        if(types.equals("3")) {
            jobs = homeService.selectJobBySalary("%" + typeName + "%", type);
        }else{
            jobs = homeService.selectJobBySalary2("%" + typeName + "%", type);
        }
        if(jobs.size() == 0 || jobs == null)
        {
            model.addAttribute("isNULL",true);
        }
        else {
            model.addAttribute("jobs", jobs);
        }
        model.addAttribute("typeName", typeName);
        model.addAttribute("types",types);
        model.addAttribute("user",session.getAttribute("user"));
        return "home/list";
    }

    @RequestMapping("searchJobByExperienceJsp")
    public String searchJobByExperience(HttpSession session,
                                        Model model,
                                        @RequestParam("typeName") String typeName,
                                        @RequestParam("type") String type,
                                        @RequestParam("types") String types)throws Exception{
        List<Job> jobs = null;
        if(types.equals("3")) {
            if (type.equals("不限")) {
                jobs = homeService.selectJobByExperienceNone("%" + typeName + "%");
            } else {
                jobs = homeService.selectJobByExperience("%" + typeName + "%", type);
            }
        }else
        {
            if (type.equals("不限")) {
                jobs = homeService.selectJobByExperienceNone2("%" + typeName + "%");
            } else {
                jobs = homeService.selectJobByExperience2("%" + typeName + "%", type);
            }
        }
        if(jobs.size() == 0 || jobs == null)
        {
            model.addAttribute("isNULL",true);
        }
        else {
            model.addAttribute("jobs", jobs);
        }
        model.addAttribute("typeName", typeName);
        model.addAttribute("types",types);
        model.addAttribute("user",session.getAttribute("user"));
        return "home/list";
    }

    @RequestMapping("searchJobByGraduationJsp")
    public String searchJobByGraduation(HttpSession session,
                                        Model model,
                                        @RequestParam("typeName") String typeName,
                                        @RequestParam("type") String type,
                                        @RequestParam("types") String types)throws Exception{
        List<Job> jobs = null;
        if(types.equals("3")) {
            if (type.equals("不限")) {
                jobs = homeService.selectJobByGraduationNone("%" + typeName + "%");
            } else {
                jobs = homeService.selectJobByGraduation("%" + typeName + "%", type);
            }
        }
        else {
            if (type.equals("不限")) {
                jobs = homeService.selectJobByGraduationNone2("%" + typeName + "%");
            } else {
                jobs = homeService.selectJobByGraduation2("%" + typeName + "%", type);
            }
        }
        if(jobs.size() == 0 || jobs == null)
        {
            model.addAttribute("isNULL",true);
        }
        else {
            model.addAttribute("jobs", jobs);
        }
        model.addAttribute("typeName", typeName);
        model.addAttribute("types",types);
        model.addAttribute("user",session.getAttribute("user"));
        return "home/list";
    }

    @RequestMapping("searchJobByPropertyJsp")
    public String searchJobByProperty(HttpSession session,
                                      Model model,
                                      @RequestParam("typeName") String typeName,
                                      @RequestParam("type") String type,
                                      @RequestParam("types") String types)throws Exception{
        List<Job> jobs = null;
        if(types.equals("3")) {
            jobs = homeService.selectJobByProperty("%" + typeName + "%", type);
        }
        else {
            jobs = homeService.selectJobByProperty2("%" + typeName + "%", type);
        }
        if(jobs.size() == 0 || jobs == null)
        {
            model.addAttribute("isNULL",true);
        }
        else {
            model.addAttribute("jobs", jobs);
        }
        model.addAttribute("typeName", typeName);
        model.addAttribute("types",types);
        model.addAttribute("user",session.getAttribute("user"));
        return "home/list";
    }

    @RequestMapping("searchJobByPublishTimeJsp")
    public String searchJobByPublishTime(HttpSession session,
                                         Model model,
                                         @RequestParam("typeName") String typeName,
                                         @RequestParam("type") String type,
                                         @RequestParam("types") String types)throws Exception{
        List<Job> jobs = null;
        if(types.equals("3")) {
            jobs = homeService.selectJobByPublishTime("%" + typeName + "%", type);
        }else {
            jobs = homeService.selectJobByPublishTime2("%" + typeName + "%", type);
        }
        if(jobs.size() == 0 || jobs == null)
        {
            model.addAttribute("isNULL",true);
        }
        else {
            model.addAttribute("jobs", jobs);
        }
        model.addAttribute("typeName", typeName);
        model.addAttribute("types",types);
        model.addAttribute("user",session.getAttribute("user"));
        return "home/list";
    }

    @RequestMapping("selectJobByAddressJsp")
    public String selectJobByAddress(HttpSession session,
                                     Model model,@RequestParam("typeName") String typeName,
                                     @RequestParam("type") String type,
                                     @RequestParam("types") String types)throws Exception{
        List<Job> jobs = null;
        System.out.println(typeName + "   "+type + "  "+ types);
        if(types.equals("3")){
            if(type.equals("全国")) {
                jobs = homeService.selectJobByExperienceNone(typeName);
            }else {
                jobs = homeService.selectJobByAddress("%" + typeName + "%", "%"+type+"%");
            }
        }else {
            if(type.equals("全国")){
                jobs = homeService.selectJobByExperienceNone2(typeName);
            }else {
                jobs = homeService.selectJobByAddress2("%" + typeName + "%", "%"+type+"%");
            }
        }
        System.out.println("size============"+jobs.size());
        if(jobs.size() == 0 || jobs == null){
            model.addAttribute("isNULL",true);
        }else {
            model.addAttribute("jobs",jobs);
        }
        model.addAttribute("typeName",typeName);
        model.addAttribute("types",types);
        model.addAttribute("user",session.getAttribute("user"));
        return "home/list";
    }

    @RequestMapping("selectJobByIdJsp")
    public String selectJobById(HttpSession session,
                                Model model,
                                @RequestParam("jobId") int jobId)throws Exception{

        System.out.println("jobId============"+jobId);

        Job job = homeService.selectJobById(jobId);
        System.out.println("jobName============"+job.getJobName());
        String email = (String) session.getAttribute("user");
        System.out.println(email);

        /*
        * 这里加上操作
        * */

        model.addAttribute("job",job);
        model.addAttribute("user",email);
        return "home/jobdetail";
    }

    @RequestMapping("logoutJsp")
    public String logout(HttpSession session)throws Exception{
        session.invalidate();
        return "home/index";
    }

    @RequestMapping("checkResume")
    public String checkResume(HttpSession session,
                              Model model,
                              @RequestParam("companyId") int companyId,
                              @RequestParam("jobId") int jobId)throws Exception{
        ResumeResult resumeResult = new ResumeResult();
        String email = (String) session.getAttribute("user");
        Delivery delivery = resumeHomeService.checkResume(email);
        if(delivery != null) {
            Pass pass = new Pass();
            pass.setJobId(jobId);
            pass.setEmail(email);
            pass.setCompanyId(companyId);
            if(resumeHomeService.returnNum(pass) == 0) {
                DeliveryActivity activity = new DeliveryActivity();
                activity.setDeliveryId(delivery.getDeliveryId());
                activity.setCustomerId(delivery.getCustomer().getCustomerId());
                activity.setCompanyId(companyId);
                activity.setsStatus(0);
                activity.setIsRead(0);
                resumeHomeService.insertResumeActivity(activity);
                System.out.println("id=====================" + activity.getId());
                resumeResult.setSuccess(true);
                resumeResult.setMsg("投递成功,点击查看");
                resumeResult.setJumpUrl("searchResume.action");
            }else{
                resumeResult.setSuccess(true);
                resumeResult.setMsg("你已投递该岗位，点击返回");
                resumeResult.setJumpUrl("returnHome.action");
            }
        }
        else {
            resumeResult.setSuccess(false);
            resumeResult.setMsg("你还没有简历，点击完善简历");
            resumeResult.setJumpUrl("returnMyResumes.action");
        }
        model.addAttribute("result",resumeResult);
        return "home/result";
    }

    @RequestMapping("searchResume")
    public String searchResume(HttpSession session,Model model)throws Exception{
        List<DeliveryActivity> deliveryActivitys = resumeHomeService.returnResumeActivity((String) session.getAttribute("user"));
        if(deliveryActivitys != null || deliveryActivitys.size() == 0)
        {
            model.addAttribute("activitys",deliveryActivitys);
            return "home/activity";
        }
        else {
            return "home/noactivity";
        }
    }


    @RequestMapping("returnHome")
    public String returnHomeJsp(HttpSession session,Model model)throws Exception{
        String user = (String) session.getAttribute("user");
        model.addAttribute("user",user);
        return "home/index";
    }


    @RequestMapping("returnMyResumes")
    public String returnMyResumes(HttpSession session,Model model)throws Exception{
        String user = (String) session.getAttribute("user");
        Delivery delivery = resumeHomeService.selectDeliveryByEmail(user);
        if(delivery != null) {
            List<WorkExp> list = delivery.getResume().getWorkExps();
            System.out.println("================" + delivery.getResume().getCustomer().getFullName());
            model.addAttribute("delivery", delivery);
            model.addAttribute("user", user);
            return "home/myResume";
        }else {
            Result result = new Result();
            result.setMsg("你还未创建简历，点击创建");
            return "home/CreateResume";
        }
    }
}
